<?php
class Controller_Admin_Statis extends Controller_Template
{
	const _TRANSACTS_TOP        = 'transacts_top';
	const _USER_BEHAVIOR		= 'user_behavior';	
	private		$_statist	= null;
	protected	$_limit		= 0;
	protected	$_statis	= null;
	protected	$top		= 0;

	public function __construct($request, $response)
	{
		$this->_limit	= 20;
		$this->_top		= 10; 
		$this->_statist	= new Statistics();
		$this->_statis	= new Model_Statis();
		$this->template = ('smarty:admin/statis/statis');
		parent::__construct($request, $response);
	}
	
	public function action_index($nowdate = null)
	{
		$this->action_total($nowdate);
	}
	
	/**
	 * 汇总数据，包含交易，用户，商品当月浏览量排行数据；
	 */
	public function action_total($date = null)
	{
		$trans_list   = array();
		$member_list  = array();
		$goods_list   = array();
		$access_shop  = array();
		$access_goods = array();

		$orders		  = array('ip','desc');
				
		$arr_time	  = $this->get_date($date);

		$start_time	  = $arr_time['start_time'];
		$end_time	  = $arr_time['end_time'];
		$start		  = $arr_time['start'];
		$end		  = $arr_time['end'];

		$member_list  = Model_Userstatis::get_formatdata($start_time, $end_time, 0, 35);
		$trans_list	  = Model_Transact::get_formate_data($start_time, $end_time);
		$goods_list   = Statistics::get_goods_statis($start,$end);
		$access_shop  = $this->format_shop_list($start,$end,$this->_top,$orders); 
		$access_goods = $this->format_goods_list($start,$end,$this->_top,$orders); 
		$rs			  = Model_Transact::get_formate_data($start_time, $end_time);
		$total_data	  = Model_Transact::transacts_total(strtotime(date('Y-m', $start_time)));

		$this->template->set('page_title', '汇总数据');
		$this->template->set('datas', $rs);
		$this->template->set('total_datas', $total_data);
		$this->template->set('month', date('Y-m', $start_time));
		$this->template->set('trans_list',$trans_list);
		$this->template->set('member_list',$member_list);
		$this->template->set('goods_list',$goods_list);
		$this->template->set('access_shop',$access_shop);
		$this->template->set('access_goods',$access_goods);
		$this->template->set('function_name','total');
	}

	public function action_members($date = null)
	{
		$arr_time	 = $this->get_date($date);

		$start_time	 = $arr_time['start_time'];
		
		$end_time	 = $arr_time['end_time'];

		$data		 = Model_Userstatis::get_formatdata($start_time, $end_time, 0, 35);

		$month_data	 = Model_Userstatis::get_month_formatdata($start_time, $end_time, 0, 35);
		
		$this->template->set('page_title', '用户数据统计');
		$this->template->set('function_name','members');
		$this->template->set('datas', $data);
		$this->template->set('month_datas', $month_data);
		$this->response->body($this->template->render());

	}

	public function action_merchants()
	{
		$arr_time	 = $this->get_date();

		$start_time	 = $arr_time['start_time'];
		
		$end_time	 = $arr_time['end_time'];
		
		$data		 = Model_Merchantstatis::get_formatdata($start_time, $end_time, 0, 10);

		$this->template->set('page_title', '用户数据统计');
		$this->template->set('function_name','merchants');
		$this->template->set('datas', $data);
		$this->response->body($this->template->render());

	}
	
	public function action_transact($date = null)
	{
		$arr_time	 = $this->get_date($date);

		$start_time	 = $arr_time['start_time'];
		
		$end_time	 = $arr_time['end_time'];
		
		$rs			 = Model_Transact::get_formate_data($start_time, $end_time);

		$rs_month	 = Model_Transact::get_formate_data_month($start_time, $end_time);
		
		$total_data	 = Model_Transact::transacts_total(strtotime(date('Y-m', $start_time)));
		
		$function_name	= $this->request->action();
		$this->template->set('function_name',$function_name);
		$this->template->set('datas', $rs);
		$this->template->set('total_datas', $total_data);
		$this->template->set('month', date('Y-m', $start_time));
		$this->template->set('month_datas', $rs_month);
		$this->template->set('page_title', '交易数据统计');
		$this->request->body($this->template->render());
	}
	
	/**
	 * 统计商品数据
	 */
    public function action_goods()
    {
		$arr_date	= $this->get_query_date($flag = 'goods');

		$start		= $arr_date['start'];
		
		$end		= $arr_date['end'];
		
		$list		= Statistics::get_goods_statis($start,$end);
		$month_list = Statistics::get_goods_statis_month();

		$list_new   = Statistics::get_goods_statis_new($start,$end);
		$month_new  = Statistics::get_goods_statis_month_new();
	
		$this->template->set('data_list',$list);
		$this->template->set('month_list',$month_list);
		$this->template->set('data_list_new',$list_new);
		$this->template->set('month_new',$month_new);
		$this->template->set('page_title', '商品操作数据概况');
		$this->template->set('function_name','goods');
	}


	/**
	 * 后台运营统计数据入口
	 */
	public function action_access()
	{
		$arr_date	= $this->get_query_date();

		$start		= $arr_date['start'];
		
		$end		= $arr_date['end'];
		
		$merchant_name = $arr_date['merchant_name'];

		$goods_name    = $arr_date['goods_name'];

		$top		= intval($this->request->query('top'));

		$top		= $top >0 ? $top : $this->_top;

		$order		= $this->request->query('order');
		
		$orders		= array('ip','desc');
		
		if($order == 'ip_asc')
			$orders = array('ip','asc');
		if($order == 'pv_desc')
			$orders = array('pv','desc');
		if($order == 'pv_asc')
			$orders = array('pv','asc');

		if(!empty($merchant_name) || !empty($goods_name)) {
			if(!empty($merchant_name)) {
				$shop_list = $this->access_shop_list_by($start,$end,$merchant_name,$top,$orders);
			}
			if(!empty($goods_name)) {
				$goods_list = $this->access_goods_list_by($start,$end,$goods_name,$top,$orders);
			}
		} else {
			$shop_list  = $this->format_shop_list($start,$end,$top,$orders); 
			$goods_list = $this->format_goods_list($start,$end,$top,$orders); 
		}
		if(!empty($shop_list))
			$shop_list	= $this->shops_statis_info($shop_list, $start, $end);	

		$this->template->set('shop_datas',$shop_list);
		$this->template->set('goods_datas',$goods_list);
		$this->template->set('top',$top);
		$this->template->set('sub_domain',Site::domain('manager'));
		$this->template->set('order',$order);
		$this->template->set('function_name','access');
	}

	private function shops_statis_info($shop_list, $start, $end)
	{
		$start		= strtotime($start);
		
		$end		= strtotime($end) + (60*60*24 - 1);
		
		$target_types	= Kohana::config('statis.target_type');
		$types			= Kohana::config('statis.type');
		$shop_fav   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['favorite']);
		$comments   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['comments']);
		$messages   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['messages']);
		$goods_fav  = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['goods'], $types['favorite']);
		$consult    = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['goods'], $types['consult']);
		$goods_favs	= $this->handle_behavior_data($goods_fav);
		$consults	= $this->handle_behavior_data($consult);
		$con_im		= Kohana::config('statistics.type.IM');
		$ims		= Model_Admin_Statis::get_statis_im_data($start, $end, $con_im);
		
		$rs	= $this->shops_transacts_top($start,$end);
		for($i=0; $i<count($shop_list); $i++)
		{
			$k = $shop_list[$i]['id'];

			$shop_list[$i]['orders'] = (int) $rs[$k]['orders'];
			$shop_list[$i]['money']  = (float) $rs[$k]['money'];
			$shop_list[$i]['shop_fav'] = (int) $shop_fav[$k]['num'];
			$shop_list[$i]['comments'] = (int) $comments[$k]['num'];
			$shop_list[$i]['goods_fav']= (int) $goods_fav[$k]['num'];
			$shop_list[$i]['messages'] = (int) $messages[$k]['num'];
			$shop_list[$i]['consults'] = (int) $consults[$k]['num'];
			$shop_list[$i]['im_pv'] = (int) $ims[$k]['pv'];
			$shop_list[$i]['im_ip'] = (int) $ims[$k]['ip'];
			$shop_list[$i]['consults_all']   = (int) ($messages[$k]['num'] 
													 + $consults[$k]['num']
													 //+ $ims[$k]['pv']
													 + $ims[$k]['ip']);

		}
		return $shop_list;	
	}

	private function shops_transacts_top($start, $end)
	{
		$data = Model_Admin_Statis::get_goods_transacts_top($start, $end, 0, 100, 2);

		$rs	= array();
		if(!empty($data))
		{
			foreach($data as $key => $val)
			{
				$k = $val['target_id'];
				$rs[$k]['orders'] = $val['orders'];
				$rs[$k]['money']  = sprintf('%.2f',$val['money']/100);
			}
		}

				
		return $rs;
	}
	//某个店铺的日流量数据
	public function action_dailyshop($shop_id)
	{
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;	

		$offset	= ($page - 1 ) * $this->_limit;

		$count	= StatisAccess::count_one_shop_data($shop_id);

		$shop_data	= StatisAccess::get_one_shop_data($shop_id, $offset, $this->_limit);
		
		$money = Model_Admin_Statis::get_transacts_by_shopid($shop_id);
		$favorite = Model_Admin_Statis::get_behavior_by_shopid($shop_id);	
		$im	= Model_Admin_Statis::get_im_consults_by_shopid($shop_id);
		for($i=0; $i<count($shop_data); $i++)
		{
			$date	= $shop_data[$i]['ddate'];

			$shop_id = $shop_data[$i]['shop_id'];

			$s_info	= Shop::shop_info($shop_id);
			
			$shop_data[$i]['merchant_id'] = $s_info['uid'];
			
			$shop_data[$i]['money']	= (int) $money[$date]['money'];
			$shop_data[$i]['orders']= (int) $money[$date]['orders'];
			$shop_data[$i]['goods'] = (int) $money[$date]['goods'];
			$shop_data[$i]['buyers']= (int) $money[$date]['buyers'];

			$shop_data[$i]['s_favorite']= (int) $favorite[$date]['s_favorite'];
			$shop_data[$i]['comments']  = (int) $favorite[$date]['comments'];
			$shop_data[$i]['messages']  = (int) $favorite[$date]['messages'];
			$shop_data[$i]['g_favorite']= (int) $favorite[$date]['g_favorite'];
			$shop_data[$i]['consult']   = (int) $favorite[$date]['consult'];
			$shop_data[$i]['consults']   = (int) ($favorite[$date]['messages'] 
												 + $favorite[$date]['consult'] 
												 + $im[$date]['uv']
												 + $im[$date]['pv']);

			$shop_data[$i]['uv'] = (int) $im[$date]['uv'];
			$shop_data[$i]['pv'] = (int) $im[$date]['pv'];

			$shop_data[$i]['pu']	= sprintf('%.2f',$shop_data[$i]['jspv']/$shop_data[$i]['jsip']);
		}
		$config	= array(
						'total_items' => $count,
						'items_per_page' => $this->_limit
				);
		
		$pagination	= Pagination::factory($config);
		$this->template->set('shop_data',$shop_data);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('page_title', Shop::name($shop_id).'日流量统计');
		$this->template->set('shop_name', Shop::name($shop_id));
		$this->template->set('total', $count);
		$this->template->set('function_name','shop_daily');
	}

	//交易排行
	public function action_transacts()
	{
		$arr_date	= $this->get_query_date();
		
		$start		= strtotime($arr_date['start']);
		
		$end		= strtotime($arr_date['end']) + (60*60*24 - 1);
		
		$type		= intval($this->request->query('type'));
			
		if($type == 2)
			$this->shops_transacts($start, $end);
		else
			$this->goods_transacts($start, $end);	
	
		$this->template->set('begin_time',date('Y-m-d', $start));
		$this->template->set('end_time',date('Y-m-d', $end));
		$this->template->set('page_title', '交易排行');
		$this->template->set('function_name','transacts');
		$this->template->set('type',$type);
		$this->response->body($this->template->render());
	}

	public function action_user_behavior()
	{
		$arr_date	= $this->get_query_date();
		
		$start		= strtotime($arr_date['start']);
		
		$end		= strtotime($arr_date['end']) + (60*60*24 - 1);

		$this->shops_data($start, $end);

		$this->template->set('begin_time',date('Y-m-d', $start));
		$this->template->set('end_time',date('Y-m-d', $end));
		$this->template->set('page_title', '用户行为统计');
		$this->template->set('function_name','user_behavior');
		$this->response->body($this->template->render());
	}

	private function shops_data($start, $end)
	{
		$shop_ids	= Model_Admin_Statis::get_behavior_shop(); 
		
		$target_types	= Kohana::config('statis.target_type');

		$types			= Kohana::config('statis.type');
		
		$shop_fav   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['favorite']);
		
		$comments   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['comments']);

		$messages   = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['shop'], $types['messages']);
		//商品收藏
		$goods_fav  = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['goods'], $types['favorite']);

		$consult    = Model_Admin_Statis::get_behavior_data_type($start, $end, $target_types['goods'], $types['consult']);

		$goods_favs	= $this->handle_behavior_data($goods_fav);
		
		$consults	= $this->handle_behavior_data($consult);

		$con_im		= Kohana::config('statistics.type.IM');

		$ims		= Model_Admin_Statis::get_statis_im_data($start, $end, $con_im);

		$rs_datas	= array();

		for($i=0; $i< count($shop_ids); $i++)
		{
			$shop_id	= $shop_ids[$i]['target_id'];
			
			$rs_datas[$shop_id]['shop_id']	= $shop_id;

			$rs_datas[$shop_id]['shop_name']		= Shop::name($shop_id);

			$rs_datas[$shop_id]['shop_favorite']	= intval($shop_fav[$shop_id]['num']);

			$rs_datas[$shop_id]['comments']			= intval($comments[$shop_id]['num']);

			$rs_datas[$shop_id]['messages']			= intval($messages[$shop_id]['num']);

			$rs_datas[$shop_id]['goods_fav']		= intval($goods_favs[$shop_id]['num']);

			$rs_datas[$shop_id]['consult']			= intval($consults[$shop_id]['num']);

			$rs_datas[$shop_id]['pv']				= intval($ims[$shop_id]['pv']);

			$rs_datas[$shop_id]['ip']		        = intval($ims[$shop_id]['ip']);
		}

		$this->template->set('datas',$rs_datas);

	}

	private function handle_behavior_data($goods_data)
	{

		$rs_data	= array();

		if(count($goods_data) <= 0)
			return $rs_data;

		foreach($goods_data as $key => $val)
		{
			$shop_id	= $val['shop_id'];

			if(! isset($rs_data[$shop_id]))
			{
				$rs_data[$shop_id]['num']	= $val['num'];
			}else{

				$rs_data[$shop_id]['num']   += $val['num'];
			}
		}
	
		return $rs_data;
	}

	//获得日期
	protected function get_date($date = null)
	{
		$month		= isset($date) ? $date : date("Ym");
		$month		.= '01';
		$en_month	= strtotime('+1 month', strtotime($month));
		
		$start_time	= strtotime($month);
		$end_time	= $en_month;
		$start      = date('Y-m-d',$start_time);
		$end        = date('Y-m-d',$end_time);

		$pre_month	= date('Ym' ,strtotime('-1 month', strtotime($month)));
		$nex_month	= date('Ym' ,strtotime('+1 month', strtotime($month)));
		$cur_month	= $month;

		$this->template->set('pre_month', $pre_month);
		$this->template->set('next_month', $nex_month);
		$this->template->set('cur_month', date('Ym', strtotime($cur_month)));
		$this->template->set('start',$start);
		$this->template->set('end',$end);

		$time	= array(
					'start_time'	=> $start_time,
					'end_time'		=> $end_time,
					'start'			=> $start,
					'end'			=> $end,
				);

		return $time;
	}

	//获得get方式传值日期
	protected function get_query_date($flag = null)
	{
		$start		= $this->request->query('start');
		
		$end		= $this->request->query('end');

		$merchant_name = $this->request->query('merchant_name');

		$goods_name = $this->request->query('goods_name');

		$time_1		= date('Y-m-d',time()-(60*60*24));
		
		$time_2		= date('Y-m-d', strtotime(date('Y-m', time())));
		
		if($flag == 'goods')
			$start	= $start =='' ? $time_2 : $start;
		else
			$start	= empty($start) ? $time_1 : $start;

		$end		= empty($end) ? $time_1 : $end;				

		$this->template->set('start',$start);
		$this->template->set('end',$end);
		$this->template->set('merchant_name',$merchant_name);
		$this->template->set('goods_name',$goods_name);

		return array('start' => $start, 'end' => $end, 'merchant_name' => $merchant_name, 'goods_name' => $goods_name);
	}

	
	protected function shops_transacts($start, $end)
	{
		$page			= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset			= $this->_limit * ($page - 1);

		$sort_type		= $this->request->query('sort_type');

		$shops_datas	= Model_Admin_Statis::get_goods_transacts_top($start, $end, $offset, $this->_limit, $type = 2, $sort_type);
		
		$domain			= Site::domain('www');
		
		for($i=0; $i< count($shops_datas); $i++)
		{
		
			$shops_datas[$i]['shop_url']	= 'http://'.$domain.'/shop/index/'.$shops_datas[$i]['target_id'];

			$shops_datas[$i]['money']		= sprintf('%.2f', $shops_datas[$i]['money']/100);

			$shops_datas[$i]['shop_name']	= Shop::name($shops_datas[$i]['target_id']);

			$shop_info	= Shop::shop_info($shops_datas[$i]['target_id']);
		
			$shops_datas[$i]['merchant']	= $shop_info['uid'];
		}
		$config	= array(
					'total_items'	=> Model_Admin_Statis::get_goods_transacts_top_count($start, $end, $type = 2),
					
					'items_per_page'=> $this->_limit,
				);
		$pagination	= Pagination::factory($config);

		$refund_status = Kohana::config('order.refund.status');
		
		$this->template->set('pagination', $pagination->render());

		$this->template->set('refund_status', array_keys($refund_status));

		$this->template->set('shops_data', $shops_datas);
	}

	protected function goods_transacts($start, $end)
	{
		$page			= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset			= $this->_limit * ($page - 1);
	
		$sort_type		= $this->request->query('sort_type');
		
		$goods_datas	= Model_Admin_Statis::get_goods_transacts_top($start, $end, $offset, $this->_limit, $type = 1, $sort_type);

		$domain			= Site::domain('www');

		for($i=0; $i< count($goods_datas); $i++)
		{
		
			$goods_datas[$i]['goods_url']	= 'http://'.$domain.'/shop/goods/'.$goods_datas[$i]['target_id'];

			$goods_datas[$i]['money']		= sprintf('%.2f', $goods_datas[$i]['money']/100);

			$query['id']	= $goods_datas[$i]['target_id'];
			
			$goods_info		= Goods::search($query);
			
			$k	= $goods_datas[$i]['target_id'];

			if(! empty($goods_info))
			{
				$goods_datas[$i]['shop_name']	= Shop::name($goods_info['list'][$k]['shop_id']);

				$shop_info                      = Shop::shop_info($goods_info['list'][$k]['shop_id']);

				$goods_datas[$i]['merchant_uid'] = $shop_info['uid'];

				$goods_datas[$i]['title']		= $goods_info['list'][$k]['title'];

				$goods_datas[$i]['shop_url']  = 'http://'.$domain.'/shop/index/'.$goods_info['list'][$k]['shop_id'];
			}
		}
		
		$config	= array(
					'total_items'	=> Model_Admin_Statis::get_goods_transacts_top_count($start, $end, $type = 1),
					'items_per_page'=> $this->_limit,
				);
		$pagination	= Pagination::factory($config);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('goods_data', $goods_datas);
	}

	/**
	 * ids search shop
	 */
	private function access_shop_list_by($start,$end,$merchant_name,$top,$orders)
	{
		$shop_list = array();
		$shop_s = Shop::search(array('shop_name'=>$merchant_name));
		if(isset($shop_s['list'])) {
			foreach( $shop_s['list'] as $shopinfo ) {
				$ids[] = $shopinfo['id'];
			}
		}

		if(empty($ids))
			return $shop_list;

		$shop_arr  = array(
			array('a.ddate','>=',$start),
			array('a.ddate','<=',$end),
			array('a.shop_id','in',$ids),
		);
		$shop_list = Statisaccess::get_type_top_list('shop',$shop_arr,0,$top,$orders,'a.shop_id');
		if(!empty($shop_list))
		{
			foreach($shop_list as & $shop)
			{
				$shop['url'] = Shop::shop_detail_url($shop['id']);
				$shop['detail_url'] = '/member/detail/'.$shop['uid'];

				$shop['pv_uv']	= sprintf('%.2f', $shop['pv']/$shop['ip']);
			}
		}
		return $shop_list;
	}

	/**
	 * ids search goods
	 */
	private function access_goods_list_by($start,$end,$goods_name,$top,$orders)
	{
		$goods_list = array();
		$goods_s = Goods::search(array('title'=>$goods_name));
		if(isset($goods_s['list'])) {
			foreach($goods_s['list'] as $goodsinfo) {
				$ids[] = $goodsinfo['goods_id'];
			}
		}

		if(empty($ids))
			return $goods_list;

		$goods_arr  = array(
			array('a.type','=',Kohana::config('statistics.type.goods')),
			array('a.ddate','>=',$start),
			array('a.ddate','<=',$end),
			array('a.itemid','in',$ids),
		);
		$goods_list = Statisaccess::get_type_top_list('goods',$goods_arr,0,$top,$orders);
		if(!empty($goods_list))
		{
			foreach($goods_list as & $goods)
			{
				$goods['url'] = Goods::goods_detail_url($goods['id']);
				$goods['detail_url'] = '/goods/detail/'.$goods['id'];
				$goods['pv_uv']		 = sprintf('%.2f', $goods['pv']/$goods['ip']);
			}
		}
		return $goods_list;
	}
	
	/**
	 * format shop_list
	 */
	private function format_shop_list($start,$end,$top,$orders)
	{
		$shop_list = array();
		$shop_arr  = array(
			array('a.ddate','>=',$start),
			array('a.ddate','<=',$end),
		);
		$shop_list = Statisaccess::get_type_top_list('shop',$shop_arr,0,$top,$orders,'a.shop_id');
		if(!empty($shop_list))
		{
			foreach($shop_list as & $shop)
			{
				$shop['url'] = Shop::shop_detail_url($shop['id']);
				$shop['detail_url'] = '/member/detail/'.$shop['uid'];

				$shop['pv_uv']	= sprintf('%.2f', $shop['pv']/$shop['ip']);
			}
		}
		return $shop_list;
	}

	/**
	 * 注水白名单
	 *
	 */
	public function action_white_paper ()
	{
		$top50 = $this->format_goods_list ('2011-09-01', '2012-03-02', 50, array('pv', 'desc'));

		$comments = new Model_Comments;
		$favorite = new Model_My_MyFavorite;
		$message  = new Model_Message;

		$shops_res= shop::search(array());
		$shops    = $suv = array();

		foreach ($shops_res['list'] as $s)
		{
			$shops[$s['id']] = $s['shop_name'];
		}


		$statis_access = new Statisaccess();
		$statis_arr = array(
				array('type', '=', Kohana::config('statistics.type.shop')),
		        array('itemid', 'in', array_keys($shops))
		);

		$rs = $statis_access->get_list($statis_arr);
		foreach ($rs as $r)
		{
			$sid = $r['shop_id'];
			$ip  = $r['ip'];
			$suv[$sid] = $ip;
		}

		$infos = array();
		foreach ($top50 as $goods)
		{
			$goods_id   = $goods['id'];
			$goods_name = $goods['name'];
			$ip         = $goods['ip'];
			$pv         = $goods['pv'];
			$url        = $goods['url'];
			$goods_uv   = $ip;

			$goods_info = goods::goods_info ($goods_id);
			$price      = Arr::get($goods_info, 'price', 0);
			$shop_id    = Arr::get($goods_info, 'shop', 0);
			$shop_name  = Arr::get($shops, $shop_id, '未知商铺');
			$shop_uv    = Arr::get($suv, $shop_id, 0);

			// 商铺收藏数
			//
			$fav_shop   = $favorite->get_count(array(
						array('itemid', '=', $shop_id),
						array('type', '=', Kohana::config('myfavorite_config.status.shop_favorite'))
						));


			// 商品收藏数
			//
			$fav_goods  = $favorite->get_count(array(
						array('itemid', '=', $goods_id),
						array('type', '=', Kohana::config('myfavorite_config.status.goods_favorite'))
						));

			// 商品已经售出的数量
			$sold = Arr::get($goods_info, 'amount_sold', 0);

			// 商品的评价数
			$cmts = $comments->get_comments_count($goods_id, Kohana::config('comments.target_type.product'));

			// 商铺的留言数
			$messages = $message->message_count('shop', $shop_id);

			// 商品的咨询数
			$consult = $message->message_count('goods', $goods_id);

			$info = array(
				'shop_name' => $shop_name,
				'shop_id'   => $shop_id,
				'shop_uv'   => $shop_uv,
				'goods_name' => $goods_name,
				'goods_id'   => $goods_id,
				'goods_uv'   => $goods_uv,
				'goods_price'=> $price / 100,
				'fav_shop'   => $fav_shop,
				'fav_goods'  => $fav_goods,
				'consult'    => $consult,
				'sold'       => $sold,
				'comments'   => $cmts,
			);

			$this->filter_goods_info ($info);
			$infos[] = $info;
		}

		array_unshift($infos, array(
				'商铺名', 
				'商铺ID',
				'商铺UV',
				'商品名',
				'商品ID',
				'商品UV',
				'商品价',
				'商铺收藏',
				'商品收藏',
				'咨询',
				'已售出',
				'已评价',
				'新商铺收藏',
				'新商品收藏',
				'新人气',
				'新咨询',
				'新已售出',
				'新评价',
			));

		$fh = fopen('/tmp/new.csv', 'w');
		foreach ($infos as $r)
		{
			fputcsv($fh, $r);
		}
		fclose($fh);
	}

	private function filter_goods_info (& $info)
	{
		$s_ratio = 1;
		$su     = $info['shop_uv'];
		$gu     = $info['goods_uv'];
		$fs     = $info['fav_shop'];
		$gs     = $info['fav_goods'];
		$sold   = $info['sold'];
		$adjust = 0;
		$consult= $info['consult'];
		$im     = 0;
		$cmts   = $info['comments'];


		$info['new_fav_shop'] = $fs + (int) ($su / 20 * $s_ratio);
		$info['new_fav_goods']= $gs + (int) ($su / 5  * $s_ratio);
		$info['access']       = $sold * 11 + $adjust * 11 + (int) ($gu / 10 * $s_ratio);
		$info['new_consult']  = $consult + $im + (int) ($gu / 13 * $s_ratio);
		$info['new_sold'   ]  = (int) ($sold / 2 + $gu / 13 * $s_ratio + $adjust);
		$info['new_comments'] = (int) ($cmts + $gu / 15 * $s_ratio);
	}

	/**
	 * format goods_list
	 */
	private function format_goods_list($start,$end,$top,$orders)
	{
		$goods_list = array();
		$goods_arr  = array(
			array('a.type','=',Kohana::config('statistics.type.goods')),
			array('a.ddate','>=',$start),
			array('a.ddate','<=',$end),
		);
		$goods_list = Statisaccess::get_type_top_list('goods',$goods_arr,0,$top,$orders);
		if(!empty($goods_list))
		{
			foreach($goods_list as & $goods)
			{
				$goods['url'] = Goods::goods_detail_url($goods['id']);
				$goods['detail_url'] = '/goods/detail/'.$goods['id'];
				$goods['pv_uv']		 = sprintf('%.2f', $goods['pv']/$goods['ip']);
			}
		}
		return $goods_list;
	}

	/**
	 * 今日数据统计
	 */
	public function action_today()
	{
		$cache = Kohana::config('cached.statis_today');
		$cache_key  = Arr::get($cache,'key');
		$cache_time = Arr::get($cache,'life', 3600);
		$tpl_path   = 'smarty:admin/statis/today';

		$content = Kohana::cache($cache_key, NULL, $cache_time);
		if (empty($content))
		{
			$today_date   = date("Y-m-d");
			$today_statis = new Model_TodayStatis();
			//$data = $today_statis->get_today_statis_data($today_date);
			$content = View::factory($tpl_path)
					->set('data',$data)
					->set('today_date',$today_date)
					->render();
			Kohana::cache($cache_key,$content,$cache_time);
		}

		$this->template->set_filename('smarty:admin/statis/statis_today');
		$this->template->set('page_data',$content);
		$this->template->set('function_name','today');
		$this->template->set('page_title','今日数据');
	}

	/**
	 * 商家订单查询页
	 */
	public function action_merchant_orders()
	{
		$start		 = date('Y-m-d', time()-30*86400);
		$end	 	 = date('Y-m-d');
		$shop_cdt    = array('open_status'=>'1');
		$shops       = Model_Shopinfo::shop_lists($shop_cdt, 0, 10000);

		$this->template->set('shops', $shops);
		$this->template->set('start',$start);
		$this->template->set('end',$end);
		$this->template->set('function_name','merchant_orders');
		$this->template->set('page_title','商家交易查询');

	}
}
?>
